<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <div th:replace="header::common"></div>
</head>
<body>
    <div class="demoTable">
        名字：
        <div class="layui-inline">
            <input class="layui-input" name="name" id="nameId" autocomplete="off">
        </div>
        邮箱：
        <div class="layui-inline">
            <input class="layui-input" name="email" id="emailId" autocomplete="off">
        </div>
        <button class="layui-btn" data-type="reload">搜索</button>
    </div>
    <table class="layui-hide" id="test" lay-filter="test"></table>

    <script type="text/html" id="toolbarDemo">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
            <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="deleteAll">批量删除</button>
        </div>
    </script>

    <script type="text/html" id="barDemo">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>

    <script type="text/html" id="imageTemplet">
        <img src="/pic/{{d.image}}"/>
    </script>

    <script>
        layui.use('table', function(){
            var table = layui.table;

            table.render({
                elem: '#test'
                ,url:'/user/list'
                ,toolbar: '#toolbarDemo'
                ,cols: [
                    [
                    {type:'checkbox', fixed: 'left'}
                    ,{field:'id', title: 'ID', sort: true, fixed: 'left'}
                    ,{field:'name', title: '用户名'}
                    ,{field:'password', title: '密码'}
                    ,{field:'nickName', title: '昵称'}
                    ,{field:'email', title: '邮箱'}
                    ,{field:'phone', title: '电话'}
                    ,{field:'image', title: '头像', templet: '#imageTemplet'}
                    ,{fixed: 'right', title:'操作', width: 125, minWidth: 125, toolbar: '#barDemo'}
                ]
                ]
                ,page: true
                ,id: 'tableId'
            });

            var $ = layui.$, active = {
                reload: function(){
                    var demoReload = $('#demoReload');

                    //执行重载
                    table.reload('tableId', {
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                        ,where: {
                            // /user/list?page=1&limit=10&name=zhangsan&email=11
                            name: $('#nameId').val(),
                            email: $('#emailId').val()
                        }
                    });
                }
            };

            $('.demoTable .layui-btn').on('click', function(){
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });

            // 工具栏事件
            table.on('toolbar(test)', function(obj){
                var id = obj.config.id;
                var checkStatus = table.checkStatus(id);
                var othis = lay(this);
                switch(obj.event){
                    case 'add':
                        layer.open({
                            type: 2,
                            area: ['650px', '450px'],
                            content: '/user/toAdd'
                        });
                        break;
                    case 'deleteAll':
                        var data = checkStatus.data;
                        //[{'id':11,'name':'zhangsan'},{'id':23,'name':'lisi'}]
                        var array = new Array();
                        for (var i = 0; i < data.length; i++) {
                            array.push(data[i].id);
                        }
                        //[11,23]
                        var ids = array.join(','); //'11,23'
                        layer.confirm('您确认要删除么?', function (){
                            //ajax 异步请求
                            $.post(
                                '/user/deleteAll', //请求地址
                                {'ids': ids},     //传递数据
                                function(result) {   //回调函数
                                    if (result.ok) {
                                        mylayer.okMsg(result.msg);
                                        //刷新表格查询最新的数据
                                        table.reload('tableId');
                                    } else {
                                        mylayer.errorMsg(result.msg);
                                    }
                                },
                                'json'              //希望返回json格式数据
                            );
                        });
                        break;
                    case 'LAYTABLE_TIPS':
                        layer.alert('Table for layui-v'+ layui.v);
                        break;
                };
            });

            //触发单元格工具事件
            table.on('tool(test)', function(obj){ // 双击 toolDouble
                var data = obj.data;
                //console.log(obj)
                if(obj.event === 'del'){
                    layer.confirm('您确认要删除么?', function (){
                        //ajax 异步请求
                        $.post(
                            '/user/deleteById', //请求地址
                            {'id': data.id},     //传递数据
                            function(result) {   //回调函数
                                console.log(result);
                                //{code: 0, msg: '删除成功', data: null, ok: true}
                                if (result.ok) {
                                    mylayer.okMsg(result.msg);
                                    //刷新表格查询最新的数据
                                    table.reload('tableId');
                                } else {
                                    mylayer.errorMsg(result.msg);
                                }
                            },
                            'json'              //希望返回json格式数据
                        );
                    });
                } else if(obj.event === 'edit'){
                    layer.open({
                        title: '编辑',
                        type: 1,
                        area: ['80%','80%'],
                        content: '<div style="padding: 16px;">自定义表单元素</div>'
                    });
                }
            });
        });
    </script>
</body>
</html>